
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SetPlayer(ply)
	if ply == nil then self:Remove() end
	self:SetPhysicsAttacker(ply)
	self:SetOwner(ply)
end

function ENT:SetDriver(ply)
	self.Driver = ply

	if ply != NULL and ply != nil then	
		if ply:IsValid() then
			local phys = self:GetPhysicsObject()
			if not phys:IsValid() then return end
			phys:EnableGravity(true)
			self:EmitSound("ambient/machines/thumper_startup1.wav", 100, math.random(90, 100))
		end
	else
		self:EmitSound("doors/doorstop1.wav", 100, math.random(90, 100))
	end
end

function ENT:Setup(model,drag,health,color,offset,parts)
	
	if self:GetOwner() == nil then return end
	
	self.Model = model
	self.Drag = drag
	self.Health = health 
	self.MaxHealth = health
	self.Color = color 
	self.Offset = offset
	self.Parts = {}
	self.Parts = parts
	
	//construct.SetPhysProp( self:GetOwner(), self, 0, nil,  { GravityToggle = 0, Material = "mat_ice" } ) 
	
end

function ENT:UpdateParts(tbl)
	self.Parts = tbl
end

function ENT:Initialize()

	self:SetModel( self.Model )
	self:SetColor( self.Color.r,self.Color.g,self.Color.b,self.Color.a )
	
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	
	self:StartMotionController( true ) //ESSENTIAL TO USE THIS
	
	self.BaseSpeed = 250 - (self.Drag * 100) //minimum is 150 speed scaled down
	self.TurnSpeed = 10 - (self.Drag * 5) //minimum is 5 speed scaled down
	self.Crash = false
	
	self.StoredAngle = self:GetTrueAngles()
	
	util.PrecacheSound("doors/doorstop1.wav")
	util.PrecacheSound("ambient/machines/thumper_startup1.wav")
	
	util.PrecacheSound("vehicles/apc/apc_idle1.wav")
	util.PrecacheSound("vehicles/airboat/fan_motor_idle_loop1.wav")
	self.DriveSounds = {}
	self.DriveSounds.Idle = CreateSound(self,"vehicles/apc/apc_idle1.wav")
	self.DriveSounds.Drive = CreateSound(self,"vehicles/apc/apc_cruise_loop3.wav")
	self.DriveSounds.CurSound = self.DriveSounds.Drive
	
end

function ENT:SetSoundLoop(snd)
	if snd == self.DriveSounds.CurSound then
		return
	else
		self.DriveSounds.CurSound:Stop()
		snd:Play()
	end
end

function ENT:EndSoundLoop()
	self.DriveSounds.CurSound:Stop()
end

function ENT:MaxVehicleHealth()
	return self.MaxHealth
end

function ENT:GetWeapon()
	return self.Weapon
end

function ENT:ExplodeEffects()
	local pos = self:GetPos()
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos+Vector(math.random(-10,10),math.random(-10,10),-250)
	tracedata.filter = self
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		util.Decal("Scorch",trace.HitPos+trace.HitNormal,trace.HitPos-trace.HitNormal)
		local effectdata = EffectData()
		effectdata:SetOrigin(trace.HitPos)
		util.Effect("smoke_crater", effectdata)
	end
	util.ScreenShake( pos, math.Round(8*self.TurnSpeed),220, math.random(3,6), 800 )  
end

function ENT:Think()
	if self:WaterLevel() > 2 then
		self.Crash = true
	end
	if self.Parts[3] and self.Weapon == nil then
		self.Weapon = self.Parts[3]
	end
	if self.Weapon and self.Driver then
		if self.Weapon.User == nil then
			self.Weapon:SetUser(self.Driver)
		end
		local aimv = self:GetTrueAngles()
		local wepang = self:LocalToWorldAngles(self.Weapon.Offset)
		local lookv = self.Driver:EyeAngles()
		self.Weapon:SetAngles(lookv + self.Weapon.Offset)
	end
end 

function ENT:OnTakeDamage(dmg)
	self:EmitSound(ChooseRandom(GameSounds.VehicleHit),100,math.random(90,110))
	if dmg:GetAttacker():IsPlayer() then
		if (dmg:GetAttacker():Team() == self:GetOwner():Team()) then
			return
		else
			self.VehHealth = self.VehHealth - dmg:GetDamage()
			umsg.Start("VehicleHealth", self:GetOwner())
			umsg.Short(self.VehHealth)
			umsg.End()
			if dmg:GetDamage() > 35 then
				local effectdata = EffectData()
				effectdata:SetOrigin(self:GetPos())
				util.Effect("prop_gib", effectdata)
			end
		end
	end
	if self.Health <= 0 then
		self:GetOwner():DestroyVehicle(dmg:GetAttacker())
	end
end 

function ENT:Destroy()
	self:EndSoundLoop()
	self:ExplodeEffects()
	for k,v in pairs(self.Parts) do
		v:Remove()
	end
	self:Remove()
end

function ENT:PhysicsCollide( data, physobj )
	if (data.Speed > 1000 && data.DeltaTime > 0.2 ) then
		if self:GetOwner() and self:GetOwner():IsValid() then
			self.Crash = true
		end
	end
end